/*
 1 展示所有订单
 2 按照status分类展示所有订单
 3 基于订单的模糊查询
 4 发货
 5 不接受退货
 */

package org.csu.goodpartner.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.csu.goodpartner.VO.OrderVO;
import org.csu.goodpartner.common.CommonResponse;
import org.csu.goodpartner.entity.Order;
import org.csu.goodpartner.persistence.InventoryMapper;
import org.csu.goodpartner.persistence.LineItemMapper;
import org.csu.goodpartner.persistence.OrderMapper;
import org.csu.goodpartner.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private InventoryMapper inventoryMapper;
    @Autowired
    private LineItemMapper lineItemMapper;

    //展示所有订单：order表
    @Override
    public CommonResponse<List<OrderVO>> getAllOrders(int shopId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("shopid",shopId);
        List<Order> orderList = orderMapper.selectList(queryWrapper);
        List<OrderVO> orderVOList = new ArrayList<OrderVO>();
        for(Order order : orderList){
            System.out.println(order.getAddrnum());
            OrderVO orderVO = orderToOrderVO(order);
            orderVO.setAddr(lineItemMapper.getAddrByAddrNum(order.getAddrnum()));
            orderVO.setPhone(lineItemMapper.getPhoneByAddrNum(order.getAddrnum()));
            orderVOList.add(orderVO);
        }
        return CommonResponse.createForSuccess(orderVOList);
    }

    //按照status分类展示所有订单
    @Override
    public CommonResponse<List<OrderVO>> getOrderByStatus(int shopId,int status){
        return CommonResponse.createForSuccess(orderMapper.selectByStatus(shopId,status));
    }

    //模糊查询
    @Override
    public CommonResponse<List<OrderVO>> getOrderByLike(int shopId,String like) {
        return CommonResponse.createForSuccess(lineItemMapper.getOrderVOByLike(like,shopId));
    }

    /*
     0：未发货；1：已发货；2：取消订单；3：退货；4：待商家确认订单；
     5：待商家确认退货；6：取消退货；7：初始值；8：待收货
    */
    //发货 0-1
    @Override
    public CommonResponse deliverOrder(int orderId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("orderid", orderId);
        //queryWrapper.eq("shopid",shopId);
        Order order = orderMapper.selectOne(queryWrapper);
        order.setStatus(1);
        orderMapper.update(order, queryWrapper);
        return CommonResponse.createForSuccessMessage("发货成功");
    }

    //不接受退货：5-6
    @Override
    public CommonResponse rejectRefund(int orderId){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("orderid", orderId);
        Order order = orderMapper.selectOne(queryWrapper);
        order.setStatus(6);
        orderMapper.update(order, queryWrapper);
        return CommonResponse.createForSuccessMessage("确认拒绝"+orderId+"号订单的退货请求");
    }

    //实体转换方法
    private OrderVO orderToOrderVO(Order order) {
        OrderVO orderVO = new OrderVO();
        orderVO.setOrderid(order.getOrderid());
        orderVO.setOrderdate(order.getOrderdate());
        orderVO.setAddrnum(order.getAddrnum());
        orderVO.setShopid(order.getShopid());
        orderVO.setStatus(order.getStatus());
        return orderVO;
    }

}
